<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            药品采购管理
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="../css/x-admin.css" media="all">
        <link rel="stylesheet" href="../css/pag.css" media="all">
        <link rel="stylesheet" href="../lib/layui/css/layui.css" media="all">
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>采购单管理</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>
        <div class="x-body">
          <form class="layui-form x-center" action="" style="width:85%">
                <div class="layui-form-pane">
                  <div class="layui-form-item">
                    <label class="layui-form-label">采购单编号</label>
                    <div class="layui-input-inline" style="width:100px">
                      <input type="text" name="name"  placeholder="药品编号" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">采购单名称</label>
                    <div class="layui-input-inline" style="width:100px">
                      <input type="text" name="name"  placeholder="药品名称" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">审核状态</label>
                    <div class="layui-input-inline" style="width:200px">
                        <select name="quiz" style="width: 200px">
                            <option value="">请选择类别</option>
                            <option value="1">未通过</option>
                            <option value="0">通过</option>
                        </select>
                    </div>

                  </div>
                </div>

            </form>
            <div class="tools">
  </div>

            <div class="layui-input-inline " style="width: 90px">
                <button class="layui-btn" id="searchEmailCompany" data-type="reload">
                    <i class="layui-icon" style="font-size: 20px; "></i> 搜索
                </button>
                <button class="layui-btn" id="createYpcg"  onclick="user_management_add('采购单创建','createYpcg.jsp','600','500')">
                    <i class="layui-icon" style="font-size: 20px; "></i> 创建采购单
                </button>
            </div>

            <div class="yys-fluid yys-wrapper">
                <div class="layui-row lay-col-space20">
                    <div class="layui-cos-xs12 layui-col-sm12 layui-col-md12 layui-col-lg12">
                        <section class="yys-body animated rotateInDownLeft">
                            <div class="yys-body-content clearfix changepwd">
                                <div class="layui-col-lg12 layui-col-md10 layui-col-sm12 layui-col-xs12" style="width:100%">
                                    <div class="user-tables">
                                        <table id="userTables" lay-filter="userTables"> </table>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>
                </div>
            </div>


</div>
<br />
<br />
<br />
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
            </div>
        </script>
        <%-- <table id="test" class="layui-table" lay-filter="test"></table>--%>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
        <script src="../lib/layui/layui.js" charset="utf-8"></script>
        <script src="../lib/layui/layui.all.js" charset="utf-8"></script>
        <script src="../js/x-layui.js" charset="utf-8"></script>
<script src="../js/jquery2.js" charset="utf-8"></script>
<script src="../js/js.js" charset="utf-8"></script>
        <script>

            function createYpcg(){
                window.location.href="${pageContext.request.contextPath}/jsp/createYpcg.jsp";
            }

            var ids=[];//存放选中的药品ID
            var currPage;
            function deletea(){
                layer.confirm('真的批量删除行么', function(index){
                    $.ajax({
                        url:'/deleteByIds',
                        method:'post',
                        data:{'ids':ids},
                        dataType:'JSON',
                        success:function () {
                            layer.close(index);
                            alert("删除成功！");

                        },
                        error:function(data){
                            alert("服务器繁忙");
                        }
                    })
                    return false;//如果不加这句，则ajax的回调函数不执行

                });
            }var a;
            layui.use(["jquery", "upload", "form", "table", "layer", "element", "laydate"], function () {
                $ = layui.jquery;
                var element = layui.element,
                    layer = layui.layer,
                    upload = layui.upload,
                    form = layui.form,
                    laydate = layui.laydate,
                    table = layui.table;
                //记录选中的数据:做缓存使用,作为参数传递给后台,然后生成pdf ,压缩
                //当前表格中的全部数据:在表格的checkbox全选的时候没有得到数据, 因此用全局存放变量
                var table_data=new Array();

                a= table.render({
                    elem: "#userTables",
                    skin: 'line', //行边框风格
                    toolbar: '#toolbarDemo',
                    cols: [[ //表头
                        {type:'checkbox', fixed: 'left',filter:'test'}
                        ,{field: 'cgNo', title: '采购单编号', width:100, sort: true}
                        ,{field: 'cgShzt', title: '审核状态', width:130}
                        ,{field: 'cgCgdmc', title: '采购单名称', width:80, sort: true}
                        ,{field: 'cgKssj', title: '开始时间', width: 160, sort: true,templet: "<div>{{layui.util.toDateString(d.yval, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                        ,{field: 'cgJssj', title: '结束时间', width: 160, sort: true,templet: "<div>{{layui.util.toDateString(d.yval, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                        ,{field: 'cgCgzt', title: '采购状态', width: 80}
                        ,{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}
                    ]]
                    ,page:  { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                        layout: [ 'prev', 'page', 'next', 'skip', 'count'] //自定义分页布局
                        ,groups: 3 //只显示 1 个连续页码
                        ,first: '首页' //不显示首页
                        ,last: '尾页' //不显示尾页
                        ,theme: '#7185a2'

                    },
                    method:'post',
                    url: "/selectYpcg",
//            data: userData,
                    even: true
                    ,done: function(res, curr, count){
                        //得到所有数据
                        console.log(res);
                        //得到当前页码
                        console.log(curr);
                        currPage=curr;
                        //得到数据总量
                        console.log(count);
                    }
                });



                //头工具栏事件
                table.on('toolbar(userTables)', function(obj){
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch(obj.event){
                        case 'getCheckData':
                            ids=[];
                            var data = checkStatus.data;
                            for ( var i = 0; i <data.length; i++) {
                                if(i!=data.length-1){
                                    ids+=data[i].ycode+',';
                                }else{
                                    ids+=data[i].ycode;
                                }
                            }
                            alert(ids);
                            layer.confirm('真的批量删除行么', function(index){
                                $.ajax({
                                    url:'/deleteByIds',
                                    method:'post',
                                    data:{'ids':ids},
                                    dataType:'JSON',
                                    success:function (res) {
                                        if(res.message==1){
                                            //obj.del();
                                            var curr = $('.layui-laypage-curr em:eq(1)').text(); // 获取当前页码   console.log(obj.tr[0]);// 获取行数据内容
                                            // $(".layui-laypage-btn").click();点击确定按钮进行刷新
                                            var array=document.getElementsByTagName('tr');
                                            if (array.length==9) { // 如是当前页的第一条数据
                                                curr = curr == 1 ? curr : curr - 1
                                            }
                                            layer.close(index);
                                            layer.alert("删除成功！");
                                            reloadTable(curr);
                                        }
                                    },
                                    error:function(data){
                                        alert("服务器繁忙");
                                    }
                                })
                                return false;//如果不加这句，则ajax的回调函数不执行

                            });
                            break;
                        case 'getCheckLength':
                            var data = checkStatus.data;
                            layer.msg('选中了：'+ data.length + ' 个');
                            break;
                        case 'isAll':
                            layer.msg(checkStatus.isAll ? '全选': '未全选');
                            break;
                    };
                });

                //监听行工具事件
                table.on('tool(userTables)', function(obj){
                    var data = obj.data;
                    //console.log(obj)
                    if(obj.event === 'del'){


                        layer.confirm('真的删除行么', function(index){
                            $.ajax({
                                url:'/deleteById',
                                method:'post',
                                data:{'ycode':data.ycode},
                                dataType:'JSON',
                                success:function(res){
                                        //obj.del();
                                        var curr = $('.layui-laypage-curr em:eq(1)').text(); // 获取当前页码   console.log(obj.tr[0]);// 获取行数据内容
                                        var dataIndex = $(obj.tr[0]).attr("data-index"); // 获取tr的data-index属性的值验证是否是当前页的第一条
                                        // $(".layui-laypage-btn").click();点击确定按钮进行刷新
                                        var array=document.getElementsByTagName('tr');
                                        if (array.length==6&&dataIndex == 0) { // 如是当前页的第一条数据
                                            curr = curr == 1 ? curr : curr - 1
                                        alert(curr);
                                        layer.close(index);
                                        layer.alert("删除成功！");
                                        reloadTable(curr);
                                    }
                                },
                                error:function(data){
                                    alert("服务器繁忙");
                                }
                            })
                            return false;//如果不加这句，则ajax的回调函数不执行


                        });
                    } else if(obj.event === 'edit'){
                        window.location.href="addYpInYpcg.jsp?cgNo="+obj.data.cgNo;
                        /*layer.prompt({
                            formType: 2
                            ,value: data.email
                        }, function(value, index){
                            obj.update({
                                email: value
                            });
                            layer.close(index);
                        });*/
                    }
                });



                //复选框选中监听,将选中的id 设置到缓存数组,或者删除缓存数组
                table.on('checkbox(userTables)', function (obj) {
                    if(obj.checked==true){
                        if(obj.type=='one'){
                            ids.push(obj.data.id);
                        }else{
                            for(var i=0;i<table_data.length;i++){
                                ids.push(table_data[i].id);
                            }
                        }
                    }else{
                        if(obj.type=='one'){
                            for(var i=0;i<ids.length;i++){
                                if(ids[i]==obj.data.id){
                                    ids.remove(i);
                                }
                            }
                        }else{
                            for(var i=0;i<ids.length;i++){
                                for(var j=0;j<table_data.length;j++){
                                    if(ids[i]==table_data[j].id){
                                        ids.remove(i);
                                    }
                                }
                            }
                        }
                    }
                });

                //表刷新方法
                var reloadTable = function (curr) {
                    var ypid=document.getElementById("ypid").value;
                    var ypname=document.getElementById("ypname").value;
                    var yplb=document.getElementById("yplb").value;
                    table.reload("userTables", { //此处是上文提到的 初始化标识id
                        page: {
                            curr:curr //重新从第 1 页开始
                        }
                        , where: {
                            ypid:ypid,
                            ypname:ypname,
                            yplb:yplb
                        }
                    });
                };



                //搜索加载--数据表格重载
                var $ = layui.$, active = {
                    reload: function () {
                        var ypid=document.getElementById("ypid").value;
                        var ypname=document.getElementById("ypname").value;
                        var yplb=document.getElementById("yplb").value;
                        //执行重载
                        table.reload('userTables', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                            , where: {
                                ypid:ypid,
                                ypname:ypname,
                                yplb:yplb
                            }
                        });
                    }
                };


                $('#searchEmailCompany').on('click', function () {
                    ids=new Array();
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
                element.init();
            });

            //删除数组自定义函数
            Array.prototype.remove=function(dx)
            {
                if(isNaN(dx)||dx>this.length){return false;}
                for(var i=0,n=0;i<this.length;i++)
                {
                    if(this[i]!=this[dx])
                    {
                        this[n++]=this[i]
                    }
                }
                this.length-=1
            }

            layui.use(['laydate','element','laypage','layer'], function(){
                $ = layui.jquery;//jquery
              lement = layui.element();//面包导航
              laypage = layui.laypage;//分页
              layer = layui.layer;//弹出层

            });

            function user_management_add(title,url,w,h){
                x_admin_show(title,url,w,h);
            }
        </script>

    </body>
</html>